package com.jiawa.wiki.controller;

import com.jiawa.wiki.req.DocQueryReq;
import com.jiawa.wiki.req.DocSaveReq;
import com.jiawa.wiki.resp.DocQueryResp;
import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.service.DocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 文档 Controller
 *
 * @author 荀彧 2021/5/17
 */
@RestController
@RequestMapping(value = "/doc")
@Slf4j
public class DocController {

    @Resource
    private DocService service;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResp<PageResp<DocQueryResp>> list(@Valid @RequestBody DocQueryReq docQueryReq) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        resp.setContent(service.list(docQueryReq));
        log.info("resp content is {}",resp.getContent());
        return resp;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResp save(@Valid @RequestBody DocSaveReq saveReq) {
        CommonResp resp = new CommonResp<>();
        service.save(saveReq);
        return resp;
    }

    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable String ids) {
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(ids.split(","));
        service.delete(list);
        return resp;
    }

    @RequestMapping(value = "/all/{ebookId}", method = RequestMethod.GET)
    public CommonResp<PageResp<DocQueryResp>> all(@PathVariable Long ebookId) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();

        PageResp<DocQueryResp> all = service.all(ebookId);
        resp.setContent(all);
        return resp;
    }

    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id) {
        CommonResp<String> resp = new CommonResp<>();
        String content = service.findContent(id);
        resp.setContent(content);
        return resp;
    }


    /**
     * 点赞功能开发
     * @param id
     * @return
     */
    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp commonResp = new CommonResp();
        service.vote(id);
        return commonResp;
    }
}

